var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass');
var reload = browserSync.reload;
var minifyCSS = require('gulp-clean-css')
var uglify = require('gulp-uglify')
var imagemin = require('gulp-imagemin')
var rename = require('gulp-rename')
var plumber = require('gulp-plumber');
var fileinclude = require('gulp-file-include');
// var $ = require('gulp-load-plugins')();
var src = {
    scss: 'code/scss/*.scss',
    css: 'code/css/*.css',
    html: 'code/*.html',
    js: 'code/js/*.js',
    images: 'code/images/*.{png,jpg,gif,ico}'
};
// 静态服务器 + 监听 scss/html 文件
gulp.task('serve', function() {

    browserSync.init({
        server: "./code/dist/"
    });

    gulp.watch("code/scss/pc/*.scss", ['pcSass']);
    gulp.watch("code/scss/mobile/*.scss", ['mobileSass']);
    gulp.watch("code/dist/css/*.css", ['pcCss']);
    gulp.watch("code/dist/mobile/css/*.css", ['mobileCss']);
    gulp.watch('code/dist/images/*.{png,jpg,gif,ico}').on('change', reload);
    gulp.watch("code/dist/js/*.js", ['script']).on('change', reload);
    gulp.watch("code/dist/mobile/js/*.js", ['script']).on('change', reload);


    gulp.watch("code/html/pc/*.html", ['pc-fileinclude']).on('change', reload);
    gulp.watch("code/include/pc/*.html", ['pc-fileinclude']).on('change', reload);
    gulp.watch("code/html/mobile/*.html", ['mobile-fileinclude']).on('change', reload);
    gulp.watch("code/include/mobile/*.html", ['mobile-fileinclude']).on('change', reload);
});

// scss编译后的css将注入到浏览器里实现更新
gulp.task('pcSass', function() {
    return gulp.src("code/scss/pc/*.scss")
        .pipe(plumber())
        .pipe(sass())
        .pipe(minifyCSS({
            advanced: false, //类型：Boolean 默认：true [是否开启高级优化（合并选择器等）]
            compatibility: 'ie7', //保留ie7及以下兼容写法 类型：String 默认：''or'*' [启用兼容模式； 'ie7'：IE7兼容模式，'ie8'：IE8兼容模式，'*'：IE9+兼容模式]
            keepBreaks: true, //类型：Boolean 默认：false [是否保留换行]
            keepSpecialComments: '*'
                //保留所有特殊前缀 当你用autoprefixer生成的浏览器前缀，如果不加这个参数，有可能将会删除你的部分前缀
        }))
        .pipe(rename({ suffix: '.min' }))
        .pipe(gulp.dest("code/dist/css"))
        .pipe(reload({ stream: true }));
});
gulp.task('mobileSass', function() {
    return gulp.src("code/scss/mobile/*.scss")
        .pipe(plumber())
        .pipe(sass())
        .pipe(minifyCSS({
            advanced: false, //类型：Boolean 默认：true [是否开启高级优化（合并选择器等）]
            compatibility: 'ie7', //保留ie7及以下兼容写法 类型：String 默认：''or'*' [启用兼容模式； 'ie7'：IE7兼容模式，'ie8'：IE8兼容模式，'*'：IE9+兼容模式]
            keepBreaks: true, //类型：Boolean 默认：false [是否保留换行]
            keepSpecialComments: '*'
                //保留所有特殊前缀 当你用autoprefixer生成的浏览器前缀，如果不加这个参数，有可能将会删除你的部分前缀
        }))
        .pipe(rename({ suffix: '.min' }))
        .pipe(gulp.dest("code/dist/mobile/css/"))
        .pipe(reload({ stream: true }));
});
gulp.task('pcCss', function() {
    return gulp.src("code/dist/css/*.css")
        .pipe(minifyCSS())
        // .pipe(rename({suffix: '.min'}))
        // .pipe(gulp.dest("dist"))
        .pipe(reload({ stream: true }));
});

gulp.task('mobileCss', function() {
    return gulp.src("code/dist/mobile/css/*.css")
        .pipe(minifyCSS())
        // .pipe(rename({suffix: '.min'}))
        // .pipe(gulp.dest("dist"))
        .pipe(reload({ stream: true }));
});
gulp.task('script', function() {
    // 1. 找到文件
    return gulp.src("code/dist/**.js")
        // 2. 压缩文件
        .pipe(plumber())
        // .pipe(uglify())
        // .pipe(rename({suffix: '.min'}))
        // 3. 另存压缩后的文件
        // .pipe(gulp.dest('./code/dist/js'))
        .pipe(reload({ stream: true }));
})

gulp.task('pc-fileinclude', function() {
    // 适配page中所有文件夹下的所有html，排除page下的include文件夹中html
    gulp.src(['./code/html/pc/*.html'])
        .pipe(fileinclude({
            prefix: '@@',
            // basepath: '@file'
        }))
        .pipe(gulp.dest('./code/dist/'))
        .pipe(reload({ stream: true }));

});
gulp.task('mobile-fileinclude', function() {
    // 适配page中所有文件夹下的所有html，排除page下的include文件夹中html
    gulp.src(['./code/html/mobile/*.html', '!./code/include/mobile/**.html'])
        .pipe(fileinclude({
            prefix: '@@',
            basepath: './code/include/pc',
            indent: true, //保留文件的缩进

        }))
        .pipe(gulp.dest('./code/dist/mobile/'))
        .pipe(reload({ stream: true }));

});
// 压缩图片任务
// 在命令行输入 gulp images 启动此任务
// gulp.task('images', function () {
//     // 1. 找到图片
//     gulp.src(src.images)
//     // 2. 压缩图片
//         .pipe($.imagemin())
//     // 3. 另存图片
//         .pipe(gulp.dest('images'))
// });


gulp.task('default', ['serve']);